package com.atguigu.demo.customer.controller;

import com.atguigu.demo.customer.bean.Customer;
import com.atguigu.demo.customer.service.CustomerService;
import com.atguigu.demo.customer.service.impl.CustomerServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  //服务访问入口类
public class CustomerController {

    @Autowired
    CustomerService customerService  ;

    @RequestMapping("/hello")  //服务访问的入口方法
    public  String helloCustomer(){
        System.out.println(" hello customer");
        return " hello customer";
    }

    //@RequestMapping(value = "/customer",method = RequestMethod.POST)  //根据id查询用户信息
    @GetMapping(  "/customer")
    public  String getCustomer(@RequestParam("id") Long customerId){
        System.out.println(" hello customer:"+customerId);
        return " hello customer"+customerId;
    }

    @GetMapping(  "/customer/{id}.html")
    public  String getCustomerDetail(@PathVariable("id") Long customerId){
        System.out.println(" hello customer detail:"+customerId);
        return " hello customer detail"+customerId;
    }

    @PostMapping(  "/customer")  //接收客户表单数据 ，保存数据库 跟据json中的key值与实体对象的属性做映射
    public  String saveCustomer( @RequestBody Customer customer){
        System.out.println(" hello customer  :"+customer);
        //customerService.saveCustomer(customer);
        // customerService.save(customer);

        customerService.saveOrUpdate(customer);// 默认使用0318库
        customer.setAge(9999);
        customerService.saveCustomer(customer);  //自定义方法写入 0224库


        return " hello customer  "+customer;
    }

    @GetMapping("/customer/by-name")
    public List getCustomerByName(@RequestParam("name") String name){
//        List<Customer> customerList = customerService.getCustomerByName(name);
//        List<Customer> list = customerService.list(); //查全部
        List<Customer> customerList = customerService.list(new QueryWrapper<Customer>().eq("name", name));
        return customerList;
    }
}
